﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Data;
using System.Runtime.Serialization;

namespace Meraz.Core.Model
{
    [PetaPoco.TableName("facebooktopic")]
    [PetaPoco.PrimaryKey("ID,BoardID", autoIncrement = false)]
    [PetaPoco.ExplicitColumns]
    [Serializable]
    [DataContract]
    public class FacebookTopic
    {
        public FacebookTopic() {
            Posts = new List<FacebookTopicPost>();
            Status = string.Empty;
            Title = string.Empty;
            TotalPosts = 1;
            UniqueUsers = 1;
            Url = string.Empty;
        }

        public static string URL_FORMAT = @"http://www.facebook.com/topic.php?uid={0}&topic={1}";

        [PetaPoco.Column]
        [DataMember]
        public int ID { get; set; }

        public string IDStr { get { return ID.ToString(); } set { int temp; if (int.TryParse(value, out temp)) ID = temp; } }

        [PetaPoco.Column]
        [DataMember]
        public long BoardID { get; set; }
        public string BoardIDStr { get { return BoardID.ToString(); } set { long temp; if (long.TryParse(value, out temp)) BoardID = temp; } }

        [PetaPoco.Column]
        [DataMember]
        public string Title { get; set; }
        public DateTime CreatedDate { get; set; }

        [DataMember]
        public string CreatedDateString
        {
            get { return CreatedDate.ToString(); }
            set
            {
                DateTime temp;
                if (DateTime.TryParse(value, out temp)) CreatedDate = temp;
            }
        }

        [PetaPoco.Column]
        public long CreatedDateTicks
        {
            get { return CreatedDate.Ticks; }
            set { CreatedDate = new DateTime(value); }
        }
        [DataMember]
        [PetaPoco.Column]
        public string Url { get; set; }

        [PetaPoco.Column]
        [DataMember]
        public string Poster { get; set; }

        [DataMember]
        [PetaPoco.Column]
        public int TotalPosts { get; set; }
        public string TotalString { get { return TotalPosts.ToString(); } set { int temp; if (int.TryParse(value, out temp)) TotalPosts = temp; } }

        [DataMember]
        [PetaPoco.Column]
        public int UniqueUsers { get; set; }
        public string UniqueUsersString { get { return UniqueUsers.ToString(); } set { int temp; if (int.TryParse(value, out temp)) UniqueUsers = temp; } }


        [DataMember]
        [PetaPoco.Column]
        public string Status { get; set; }

        [DataMember]
        [PetaPoco.Column]
        public string Bad { get; set; }

        [DataMember]
        [PetaPoco.Column]
        public string HasLink { get; set; }

        [DataMember]
        public List<FacebookTopicPost> Posts { get; set; }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("{");
            builder.AppendFormat("{0}:{1},", "ID", ID);
            builder.AppendFormat("{0}:{1},", "BoardID", BoardID);
            builder.AppendFormat("{0}:{1},", "Title", Title);
            builder.AppendFormat("{0}:{1},", "CreatedDate", CreatedDateString);
            builder.AppendFormat("{0}:{1},", "Url", Url);
            builder.AppendFormat("{0}:{1}", "TotalPosts", TotalPosts);
            builder.AppendFormat("{0}:{1}", "UniqueUsers", UniqueUsers);
            builder.Append("}");
            return builder.ToString();
        }


        public string GetLink()
        {
            return string.Format("<a href='{0}' target='_blank'>{1}</a>", Url, Title);
        }
    }
}
